/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_TMenu.h"
using namespace frm;

// TPopupMenu /////////////////////////////////////////////////////////////////

//
void TPopupMenu::THMenu::Create()
{
  HMenu = ::CreatePopupMenu();
  
/*************
  ::MENUINFO Info; 
  Info.cbSize = sizeof(::MENUINFO); 
  Info.fMask = MIM_MENUDATA | MIM_STYLE;
  ::GetMenuInfo(HMenu,&Info); 
  Info.dwStyle = MNS_NOTIFYBYPOS;
  Info.dwMenuData = (ULONG_PTR)NULL;
  ::SetMenuInfo(HMenu,&Info);
*****************/  
}

//
void TPopupMenu::Popup(control::TWindow &Window,grf::TPoint PtClient,svl::TCommandArray *NewItems,bool EnDeleteNewItems)
{
  Window.PanelPaint.EndAll();

  grf::TPoint PtScreen = Window.ClientToScreen(PtClient);
  svl::TArray A; THMenu *P = new THMenu; A.Add(P,true); 

/************************
::MENUINFO Info; 
Info.cbSize = sizeof(::MENUINFO); 
Info.fMask = MIM_MENUDATA | MIM_STYLE;

::GetMenuInfo(P->HMenu,&Info); 
Info.dwStyle = MNS_NOTIFYBYPOS;
Info.dwMenuData = (ULONG_PTR)NULL;//&I;
::SetMenuInfo(P->HMenu,&Info);
****************************/

  Load(A,*P,*NewItems);

  //::SetCursor(::LoadCursor(NULL,IDC_ARROW));
  svl::TCommand *Item = (svl::TCommand*)::TrackPopupMenu(
    P->HMenu, TPM_RETURNCMD | TPM_LEFTALIGN | TPM_LEFTBUTTON, PtScreen.X,PtScreen.Y, 0, Window.Handle(), NULL
  );
  
  if (Item) { 
    Window.UpdateWindow(); Item->Execute();
  }
  if (EnDeleteNewItems) {
    delete NewItems;
  }

/*********************
  struct TPostClick : public svl::TPostMessage
  {
    TMenuItem *item;
    TMenuItemArray *newItems;
    //
    TPostClick(TMenuItem *Item,TMenuItemArray *NewItems) { item = Item; newItems = NewItems; Post();}
    //
    virtual void TPostMessage_Receive() { item->Click(); delete newItems;}
  };

  if (Item) { new TPostClick(Item,NewItems);}
  else      { delete NewItems;}
**********************/
}

//
void TPopupMenu::Load(svl::TArray &A,THMenu &M,svl::TCommandArray &Items)
{
  for (int N = 0; N < Items.Count(); N++)
  {
    svl::TCommand &I = Items.Get(N); 
    
    if (I.Children.Count()) { 
      THMenu *P = new THMenu; A.Add(P,true); 

/*******************
::MENUINFO Info; 
Info.cbSize = sizeof(::MENUINFO); 
Info.fMask = MIM_MENUDATA | MIM_STYLE;

::GetMenuInfo(P->HMenu,&Info); 
Info.dwStyle = MNS_NOTIFYBYPOS;
Info.dwMenuData = (ULONG_PTR)&I;
::SetMenuInfo(P->HMenu,&Info);
*******************/

      ::AppendMenu(
        M.HMenu, 
        //MF_POPUP|MF_ENABLED, 
        MF_POPUP | (I.TCommand_GetEnable() ? MF_ENABLED : MF_DISABLED|MF_GRAYED), 
        (UINT_PTR)P->HMenu ,I.Caption
      );
      Load(A,*P,I.Children); 
    } 
    else  { 
      ::AppendMenu(
        M.HMenu, 
        //MF_STRING|MF_ENABLED, 
        MF_STRING | (I.TCommand_GetEnable() ? MF_ENABLED : MF_DISABLED|MF_GRAYED), 
        (UINT_PTR)&I, 
        I.Caption.CompareIC("-") ? NULL : I.Caption.S()
      ); 
    }
  }
}

// TMainMenu //////////////////////////////////////////////////////////////////

//
TMainMenu::TMainMenu()
{
}

//
void TMainMenu::SetToWindow(control::TWindow &Window)
{
  a.Clear(); 
  menu.Clear(); 
  a.Add(&menu,false); 
  Load(a,menu,Items);
  ::SetMenu(Window,menu.HMenu);
}

//
void TMainMenu::Load(svl::TArray &A,THMenu &M,svl::TCommandArray &Items)
{
  for (int N = 0; N < Items.Count(); N++)
  {
    svl::TCommand &I = Items.Get(N); 
    
    if (I.Children.Count()) { 
      THMenu *P = new THMenu; A.Add(P,true); 

      ::MENUINFO Info; 
      Info.cbSize = sizeof(::MENUINFO); 
      Info.fMask = MIM_MENUDATA | MIM_STYLE;

      ::GetMenuInfo(P->HMenu,&Info); 
      Info.dwStyle = MNS_NOTIFYBYPOS;
      Info.dwMenuData = (ULONG_PTR)&I;
      ::SetMenuInfo(P->HMenu,&Info);

      ::AppendMenu(
        M.HMenu, 
        //MF_POPUP | (I.Enabled ? MF_ENABLED : MF_DISABLED|MF_GRAYED), 
        MF_POPUP|MF_ENABLED, 
        (UINT_PTR)P->HMenu ,I.Caption
      );

      Load(A,*P,I.Children); 

/**************
      ::MENUINFO Info1; 
      Info1.cbSize = sizeof(::MENUINFO); 
      Info1.fMask = MIM_MENUDATA;
      ::GetMenuInfo(P->HMenu,&Info1); 
      int N=1;
***************/      
    } 
    else  { 
      ::AppendMenu(
        M.HMenu, 
        //MF_STRING | (I.Enabled ? MF_ENABLED : MF_DISABLED|MF_GRAYED), 
        MF_STRING|MF_ENABLED, 
        (UINT_PTR)&I, 
        I.Caption.CompareIC("-") ? NULL : I.Caption.S()
      ); 
    }
  }
}

//
void TMainMenu::THMenu::Create()
{
  HMenu = ::CreateMenu();

  ::MENUINFO Info; 
  Info.cbSize = sizeof(::MENUINFO); 
  Info.fMask = MIM_MENUDATA | MIM_STYLE;
  ::GetMenuInfo(HMenu,&Info); 
  Info.dwStyle = MNS_NOTIFYBYPOS;
  Info.dwMenuData = (ULONG_PTR)NULL;
  ::SetMenuInfo(HMenu,&Info);
}
